#!/bin/bash

# 安装redis服务
function redis_install(){
  export DEBIAN_FRONTEND=noninteractive
  (yum --version &> /dev/null && yum -y install redis-server) || (apt --version &> /dev/null && apt-get install  -q -y redis-server) || exit 0 
  # 配置密码
  grep 'd166be68-ed5e-4689-890f-ec4df6aee4ad' /etc/redis/redis.conf > /dev/null || \ 
  echo "requirepass 'd166be68-ed5e-4689-890f-ec4df6aee4ad'" >> /etc/redis/redis.conf && \ 
  systemctl restart redis

   . .env/bin/activate
  pip list | grep django-redis > /dev/null || pip install django-redis==5.4.0
}

function redis_config(){
   # django 配置使用redis存储会话
  grep ^CACHES vmbox-api/application/settings.py > /dev/null || \
  echo -e "
    SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
    SESSION_CACHE_ALIAS = 'default'
    CACHES = {
        'default': {
            'BACKEND': 'django_redis.cache.RedisCache',
            'LOCATION': 'redis://127.0.0.1:6379/0',
            'OPTIONS': {
                'CLIENT_CLASS': 'django_redis.client.DefaultClient',
                'PASSWORD': 'd166be68-ed5e-4689-890f-ec4df6aee4ad'
            }
        }
    }" | sed 's/^\    //g' >> vmbox-api/application/settings.py
}

# 数据库迁移
function migrate(){
  cd vmbox-api
  python3 run.py makemigrations $1
  python3 run.py migrate
  cd ..
}

# 初始化用户
function init_user(){
  cd vmbox-api
  echo -e '
    from application.models import User
    u = User()
    u.username = "admin"
    u.set_password("admin@gtinlian")
    u.email = "gtinlian@sina.com"
    u.save()
  ' | sed 's/\  //g' | python3 run.py shell
  cd ..
}

#centos7 环境配置
function centOS_install(){
  yum -y update
  yum -y install elrepo-release
  yum -y install python3          # 项目使用python 3 开发
  yum -y install libvirt          # libvirt 管理服务 
  yum -y install qemu-kvm         # key 环境
  yum -y install qemu-img         # kvm 环境管理磁盘
  yum install -y libvirt-devel       # libvirt-python 依赖
  yum -y install mariadb-devel      # mysqlclient 依赖
  yum -y install gcc gcc-c++
  yum -y install wget

  # 依赖配置
  echo 'asgiref==3.4.1
    certifi==2024.2.2
    charset-normalizer==2.0.12
    Django==3.2.25
    idna==3.6
    libvirt-python==10.2.0
    mysqlclient==2.1.1
    pytz==2024.1
    requests==2.27.1
    sqlparse==0.4.4
    typing_extensions==4.1.1
    opencv-python>=4.10.0.82
    django-apscheduler==0.6.2
    urllib3==1.26.18' | sed 's/\  //g' > /tmp/requirement.txt
    

  python3 -m venv .env
  . .env/bin/activate
  # 更新pip
  pip install --upgrade pip
  pip install -r /tmp/requirement.txt -i https://mirrors.aliyun.com/pypi/simple

  wget https://nodejs.org/dist/v16.20.2/node-v16.20.2-linux-x64.tar.xz
  tar -xf node-v16.20.2-linux-x64.tar.xz
  cat /etc/profile |grep 'node-v16.20.2-linux-x64' &>/dev/null || echo 'export PATH=${PATH}:'`pwd`/node-v16.20.2-linux-x64/bin >> /etc/profile
}

# ubuntu23 环境环境配置
function ubuntu_install(){
  export DEBIAN_FRONTEND=noninteractive
  apt-get update
  # kvm 环境安装
  apt-get -q -y install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils 
  # 安装 编译工具,nginx 依赖
  apt-get install -q -y gcc g++ make libssh-dev libpcre3-dev
  # 安装nodejs npm 工具
  apt-get -q -y install nodejs npm 
  #python 依赖 环境搭建
  apt-get -q -y install  python3-venv libvirt-dev libmysqlclient-dev python3-dev
  
  # 依赖配置
  echo 'Django==5.0.1
    sqlparse==0.4.4
    libvirt-python==9.10.0
    mysqlclient==2.2.1
    requests==2.31.0
    opencv-python>=4.10.0.82
    django-redis==5.4.0
    django-apscheduler==0.6.2
    ' | sed 's/^\    //g' > /tmp/requirement.txt

  python3 -m venv .env
  . .env/bin/activate
  pip install --upgrade pip
  pip install -r /tmp/requirement.txt -i https://mirrors.aliyun.com/pypi/simple
  rm -rf /tmp/requirement.txt

  # 安装数据库
  apt-get -q -y  install mysql-server 
  echo -e "create user gtinlian@'localhost';grant all on *.* to gtinlian@'localhost';
  alter user gtinlian@'localhost' identified by 'd166be68-ed5e-4689-890f-ec4df6aee4ad';
  create database vmbox default charset=utf8;"  | mysql 

  migrate
  migrate virtapp
  init_user
  
}

#nginx安装部署
function install_nginx(){
  if [ -d "nginx" ];then
    echo "nginx 已经安装"
  else
    cd ./nginx-1.22.1
    ./install.sh
    cd ..
  fi
}

#前端项目编译部署
function install_web(){
  cd vmbox-web
  npm install && npm run build
  cd ..
  sed -i "s/src=\"\//src=\"/g" nginx/html/vmbox/index.html
  sed -i "s/href=\"\//href=\"/g" nginx/html/vmbox/index.html
}

function install_ws_scrcpy(){
  cd ws-scrcpy
  npm install
  cd ..
}
# 命令 
# web重新编译前端
# migrate 迁移数据库
case $1 in

  web)  
    rm -rf nginx/html/vmbox
    install_web
  ;;

  migrate)
    migrate $2
    ;;

  redis)
    (redis-server --version &> /dev/null && echo "redis 已经安装!") || ( redis_install && redis_config )
  ;;

  redis-config)
    redis_config
  ;;

  install)
    # 根据系统进行环境搭建
    case $(sed '$a echo ${PRETTY_NAME}' /etc/os-release  | sh) in 
      'Ubuntu 23.04')
       	ubuntu_install
       ;;
      'Ubuntu 22.04.4 LTS')
        ubuntu_install
      ;;
      'Ubuntu 22.04.2 LTS')
        ubuntu_install
      ;;
      'CentOS Linux 7 (Core)')
        centOS_install
      ;;
    esac

    #redis_install
    #redis_config
    install_nginx
    install_web
  ;;

  *)
    echo -e "
    install option
    option:
        web             编译前端项目
        migrate         数据库迁移
        redis           安装redis
        redis-config    django项目配置redis
        install         安装
        "
  ;;
esac
